package handler

import (
	"context"
	"easy-note/gateway/utils/resp"
	"easy-note/user/kitex_gen/user"
	userClient "easy-note/user/kitex_gen/user/user"
	"github.com/cloudwego/hertz/pkg/app"
)

type UserHandler struct{}

var UserClient userClient.Client

func (userHandler *UserHandler) Register(ctx context.Context, c *app.RequestContext) {
	var registerForm struct {
		Username, Password, Avatar string
		Gender                     int32
	}
	err := c.BindAndValidate(&registerForm)
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	result, err := UserClient.InsertUser(ctx, &user.ReqInsertUser{
		Username: registerForm.Username,
		Password: registerForm.Password,
		Avatar:   registerForm.Avatar,
		Gender:   registerForm.Gender,
	})
	if !result.GetIsExist() {
		resp.BadRequestResponse(c, 2, "注册失败，请检查您输入")
	}
	resp.SuccessResponse(c, "注册成功", nil)
}

func (userHandler *UserHandler) GetUserByUsername(_ context.Context, c *app.RequestContext) {
	user2, ok := c.Get("user")
	if !ok {
		resp.BadRequestResponse(c, 6, "请求失败")
	}
	if user2 == nil {
		resp.NotFountResponse(c, "找不到该用户")
		return
	}
	resp.SuccessResponse(c, "请求成功", user2)

}
